package com.puboot.module.blog.controller;

import com.puboot.common.util.*;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/chemicalindustry/api")
@AllArgsConstructor
public class ChemicalIndustryController {

    private final HttpServletRequest httpServletRequest;
    private final ChemicalIndustryService chemicalIndustryService;
    private final ChemicalIndustryDetailService chemicalIndustryDetailService;
    private final ChemicalIndustryAdipicAcidService chemicalIndustryAdipicAcidService;
    private final ChemicalIndustryCarbonateService chemicalIndustryCarbonateService;
    private final ChemicalIndustryNitricAcidService chemicalIndustryNitricAcidService;
    private final ChemicalIndustryRawMaterialService chemicalIndustryRawMaterialService;
    private final ChemicalIndustryOtherCarbonContentService chemicalIndustryOtherCarbonContentService;
    private final ChemicalIndustryCarbonContentService chemicalIndustryCarbonContentService;
    private final ChemicalIndustryAllResultService chemicalIndustryAllResultService;
    private final ChemicalIndustryAllService chemicalIndustryAllService;


    @PostMapping("/result")
    @Transactional
    public ResponseVo result(ChemicalIndustry chemicalIndustry) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> ncvs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getNcv())) {
                ncvs = Arrays.asList(chemicalIndustry.getNcv().split(","));
            }
            List<String> fcs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getFc())) {
                fcs = Arrays.asList(chemicalIndustry.getFc().split(","));
            }
            List<String> ccs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getCc())) {
                ccs = Arrays.asList(chemicalIndustry.getCc().split(","));
            }
            List<String> ofs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getOf())) {
                ofs = Arrays.asList(chemicalIndustry.getOf().split(","));
            }
            List<String> adirs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getAdirs())) {
                adirs = Arrays.asList(chemicalIndustry.getAdirs().split(","));
            }
            List<String> ccirs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getCcirs())) {
                ccirs = Arrays.asList(chemicalIndustry.getCcirs().split(","));
            }
            List<String> adips = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getAdips())) {
                adips = Arrays.asList(chemicalIndustry.getAdips().split(","));
            }
            List<String> ccips = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getCcips())) {
                ccips = Arrays.asList(chemicalIndustry.getCcips().split(","));
            }
            List<String> adiws = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getAdiws())) {
                adiws = Arrays.asList(chemicalIndustry.getAdiws().split(","));
            }
            List<String> cciws = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getCciws())) {
                cciws = Arrays.asList(chemicalIndustry.getCciws().split(","));
            }
            List<String> carbonateAdijs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getCarbonateAdijs())) {
                carbonateAdijs = Arrays.asList(chemicalIndustry.getCarbonateAdijs().split(","));
            }

            List<String> carbonateEfijs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getCarbonateEfijs())) {
                carbonateEfijs = Arrays.asList(chemicalIndustry.getCarbonateEfijs().split(","));
            }
            List<String> carbonatePurijs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getCarbonatePurijs())) {
                carbonatePurijs = Arrays.asList(chemicalIndustry.getCarbonatePurijs().split(","));
            }

            List<String> nitricAcidAdijs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getNitricAcidAdijs())) {
                nitricAcidAdijs = Arrays.asList(chemicalIndustry.getNitricAcidAdijs().split(","));
            }
            List<String> nitricAcidNitrousOxideEfijs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getNitricAcidNitrousOxideEfijs())) {
                nitricAcidNitrousOxideEfijs = Arrays.asList(chemicalIndustry.getNitricAcidNitrousOxideEfijs().split(","));
            }

            List<String> nitricAcidNitrousOxideRemoves = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getNitricAcidNitrousOxideRemoves())) {
                nitricAcidNitrousOxideRemoves = Arrays.asList(chemicalIndustry.getNitricAcidNitrousOxideRemoves().split(","));
            }
            List<String> nitricAcidNitrousOxideRatios = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getNitricAcidNitrousOxideRatios())) {
                nitricAcidNitrousOxideRatios = Arrays.asList(chemicalIndustry.getNitricAcidNitrousOxideRatios().split(","));
            }

            List<String> adipicAcidAdijs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getAdipicAcidAdijs())) {
                adipicAcidAdijs = Arrays.asList(chemicalIndustry.getAdipicAcidAdijs().split(","));
            }
            List<String> adipicAcidEfijs = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getAdipicAcidEfijs())) {
                adipicAcidEfijs = Arrays.asList(chemicalIndustry.getAdipicAcidEfijs().split(","));
            }

            List<String> adipicAcidNitrousOxideRemoves = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getAdipicAcidNitrousOxideRemoves())) {
                adipicAcidNitrousOxideRemoves = Arrays.asList(chemicalIndustry.getAdipicAcidNitrousOxideRemoves().split(","));
            }
            List<String> adipicAcidNitrousOxideRatios = new ArrayList<>();
            if (StringUtils.isNotBlank(chemicalIndustry.getAdipicAcidNitrousOxideRatios())) {
                adipicAcidNitrousOxideRatios = Arrays.asList(chemicalIndustry.getAdipicAcidNitrousOxideRatios().split(","));
            }

            Double eBurn = SteelUtil.getEBurn(ncvs, fcs, ccs, ofs);
            Double eProcess = ChemicalIndustryUtil.getEProcess(adirs, ccirs,
                    adips, ccips,
                    adiws, cciws,
                    carbonateAdijs, carbonateEfijs, carbonatePurijs, nitricAcidAdijs,
                    nitricAcidNitrousOxideEfijs, nitricAcidNitrousOxideRemoves, nitricAcidNitrousOxideRatios,
                    adipicAcidAdijs, adipicAcidEfijs, adipicAcidNitrousOxideRemoves, adipicAcidNitrousOxideRatios);
            Double rco2 = GasDCUtil.allMul(19.77, chemicalIndustry.getQi(), chemicalIndustry.getPurco2()).doubleValue();
            Double ePurchaseElectric = SteelUtil.getEPurchaseElectric(chemicalIndustry.getAdPurchaseElectric(), chemicalIndustry.getEfPurchaseElectric());
            Double ePurchaseHeat = SteelUtil.getEPurchaseHeatOrEOutputHeat(chemicalIndustry.getAdPurchaseHeat());
            Double eOutputHeat = SteelUtil.getEPurchaseHeatOrEOutputHeat(chemicalIndustry.getAdOutInputHeat());
            Double eOutputElectric = SteelUtil.getEOutputElectric(chemicalIndustry.getAdOutInputElectric(), chemicalIndustry.getEfOutInputElectric());

            Double result = ChemicalIndustryUtil.getE(eBurn, eProcess, rco2, ePurchaseElectric,
                    ePurchaseHeat, eOutputElectric, eOutputHeat);

            chemicalIndustry.setCustomerId(customer.getId());
            chemicalIndustry.setCreateTime(new Date());
            chemicalIndustry.setEBurn(GasDCUtil.round(eBurn, 2));
            chemicalIndustry.setEProcess(GasDCUtil.round(eProcess, 2));
            chemicalIndustry.setEPurchaseElectric(GasDCUtil.round(ePurchaseElectric, 2));
            chemicalIndustry.setEPurchaseHeat(GasDCUtil.round(ePurchaseHeat, 2));
            chemicalIndustry.setEOutputHeat(GasDCUtil.round(eOutputHeat, 2));
            chemicalIndustry.setRco2(GasDCUtil.round(rco2, 2));
            chemicalIndustry.setEOutputElectric(GasDCUtil.round(eOutputElectric, 2));
            chemicalIndustry.setState(Constants.STATE_INVAILD);
            chemicalIndustry.setResult(GasDCUtil.getThousandth(result));
            chemicalIndustryService.save(chemicalIndustry);
            for (int i = 0; i < ncvs.size(); i++) {
                ChemicalIndustryDetail detail = new ChemicalIndustryDetail();
                detail.setNcv(Double.valueOf(ncvs.get(i)));
                detail.setFc(Double.valueOf(fcs.get(i)));
                detail.setCc(Double.valueOf(ccs.get(i)));
                detail.setOf(Double.valueOf(ofs.get(i)));
                detail.setChemicalIndustryId(chemicalIndustry.getId());
                chemicalIndustryDetailService.save(detail);
            }
            for (int i = 0; i < adirs.size(); i++) {
                ChemicalIndustryRawMaterial rawMaterial = new ChemicalIndustryRawMaterial();
                rawMaterial.setAdir(Double.valueOf(adirs.get(i)));
                rawMaterial.setCcir(Double.valueOf(ccirs.get(i)));
                rawMaterial.setChemicalIndustryId(chemicalIndustry.getId());
                chemicalIndustryRawMaterialService.save(rawMaterial);
            }
            for (int i = 0; i < adips.size(); i++) {
                ChemicalIndustryCarbonContent carbonContent = new ChemicalIndustryCarbonContent();
                carbonContent.setAdip(Double.valueOf(adips.get(i)));
                carbonContent.setCcip(Double.valueOf(ccips.get(i)));
                carbonContent.setChemicalIndustryId(chemicalIndustry.getId());
                chemicalIndustryCarbonContentService.save(carbonContent);
            }
            for (int i = 0; i < adiws.size(); i++) {
                ChemicalIndustryOtherCarbonContent carbonContent = new ChemicalIndustryOtherCarbonContent();
                carbonContent.setAdiw(Double.valueOf(adiws.get(i)));
                carbonContent.setCciw(Double.valueOf(cciws.get(i)));
                carbonContent.setChemicalIndustryId(chemicalIndustry.getId());
                chemicalIndustryOtherCarbonContentService.save(carbonContent);
            }
            for (int i = 0; i < adipicAcidAdijs.size(); i++) {
                ChemicalIndustryAdipicAcid adipicAcid = new ChemicalIndustryAdipicAcid();
                adipicAcid.setAdipicAcidAdij(Double.valueOf(adipicAcidAdijs.get(i)));
                adipicAcid.setAdipicAcidNitrousOxideEfij(Double.valueOf(adipicAcidEfijs.get(i)));
                adipicAcid.setAdipicAcidNitrousOxideRatio(Double.valueOf(adipicAcidNitrousOxideRatios.get(i)));
                adipicAcid.setAdipicAcidNitrousOxideRemove(Double.valueOf(adipicAcidNitrousOxideRemoves.get(i)));
                adipicAcid.setChemicalIndustryId(chemicalIndustry.getId());
                chemicalIndustryAdipicAcidService.save(adipicAcid);
            }
            for (int i = 0; i < carbonateAdijs.size(); i++) {
                ChemicalIndustryCarbonate carbonate = new ChemicalIndustryCarbonate();
                carbonate.setCarbonateAdij(Double.valueOf(carbonateAdijs.get(i)));
                carbonate.setCarbonateEfij(Double.valueOf(carbonateEfijs.get(i)));
                carbonate.setCarbonatePurij(Double.valueOf(carbonatePurijs.get(i)));
                carbonate.setChemicalIndustryId(chemicalIndustry.getId());
                chemicalIndustryCarbonateService.save(carbonate);
            }
            for (int i = 0; i < nitricAcidAdijs.size(); i++) {
                ChemicalIndustryNitricAcid nitricAcid = new ChemicalIndustryNitricAcid();
                nitricAcid.setNitricAcidAdij(Double.valueOf(nitricAcidAdijs.get(i)));
                nitricAcid.setNitricAcidNitrousOxideEfij(Double.valueOf(nitricAcidNitrousOxideEfijs.get(i)));
                nitricAcid.setNitricAcidNitrousOxideRatio(Double.valueOf(nitricAcidNitrousOxideRatios.get(i)));
                nitricAcid.setNitricAcidNitrousOxideRemove(Double.valueOf(nitricAcidNitrousOxideRemoves.get(i)));
                nitricAcid.setChemicalIndustryId(chemicalIndustry.getId());
                chemicalIndustryNitricAcidService.save(nitricAcid);
            }
            return ResultUtil.success("计算成功", chemicalIndustry);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }

    @PostMapping("/resulttwo")
    @Transactional
        public ResponseVo result(String result, String ids) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> results = new ArrayList<>();
            if (StringUtils.isNotBlank(result)) {
                results = Arrays.asList(result.split("@"));
            }

            List<String> idList = new ArrayList<>();
            if (StringUtils.isNotBlank(ids)) {
                idList = Arrays.asList(ids.split(","));
            }
            BigDecimal r = new BigDecimal(0.0);
            for (int i = 0; i < results.size(); i++) {
                String rg = results.get(i).replaceAll(",", "");
                BigDecimal jsgc = new BigDecimal(rg);
                r = r.add(jsgc);
            }
            ChemicalIndustryAllResult allResult = new ChemicalIndustryAllResult();
            allResult.setCustomerId(customer.getId());
            allResult.setResult(GasDCUtil.getThousandth(r.doubleValue()));
            allResult.setState(Constants.STATE_VAILD);
            allResult.setCreateTime(new Date());
            chemicalIndustryAllResultService.save(allResult);
            for (int i = 0; i < idList.size(); i++) {
                ChemicalIndustryAll all = new ChemicalIndustryAll();
                all.setChemicalIndustryId(allResult.getId());
                all.setChemicalIndustryId(Integer.valueOf(idList.get(i)));
                chemicalIndustryAllService.save(all);
            }

            return ResultUtil.success("计算成功", allResult);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }

}
